//package cn.lysoy.demo.dynamicDatasourceTest;///**
//
//
//import com.alibaba.druid.pool.DruidDataSource;
//import com.mysql.cj.jdbc.MysqlXADataSource;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.mybatis.spring.SqlSessionFactoryBean;
//import org.mybatis.spring.SqlSessionTemplate;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//
//import javax.sql.DataSource;
//import java.util.HashMap;
//import java.util.Map;
//

//@Configuration
//@Slf4j
//public class DynamicDatasourceConfig {
//    /**
//     * @return DataSource
//     */
//    @Bean("masterDatasource")
//    @Primary
//    public DataSource masterDataSource() {
//        final DruidDataSource druidDataSource = new DruidDataSource();
//        druidDataSource.setUrl("jdbc:mysql:///mall?useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true&allowPublicKeyRetrieval=true&useServerPrepStmts=true&cachePrepStmts=true");
//        druidDataSource.setPassword("123456");
//        druidDataSource.setUsername("root");
//        druidDataSource.setMaxActive(2);
//        druidDataSource.setMinIdle(2);
//        druidDataSource.setInitialSize(2);
//        log.info("初始化主数据库成功");
//        return druidDataSource;
//    }
//
//    /**
//     * @return DataSource
//     */
//    @Bean("slaveDatasource")
//    public DataSource slaveDataSource() {
//        final DruidDataSource druidDataSource = new DruidDataSource();
//        druidDataSource.setUrl("jdbc:mysql:///log?useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true&allowPublicKeyRetrieval=true&useServerPrepStmts=true&cachePrepStmts=true");
//        druidDataSource.setPassword("123456");
//        druidDataSource.setUsername("root");
//        druidDataSource.setMaxActive(2);
//        druidDataSource.setMinIdle(2);
//        druidDataSource.setInitialSize(2);
//        return druidDataSource;
//    }
//
//    @Bean(name = "masterSqlSessionFactory")
//    @Primary
//    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDatasource") DataSource dataSource) throws Exception {
//        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//        bean.setDataSource(dataSource);
//        return bean.getObject();
//    }
//
//
//    @Bean(name = "masterSqlSessionTemplate")
//    @Primary
//    public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
//        return new SqlSessionTemplate(sqlSessionFactory);
//    }
//
//    @Bean(name = "slaveSqlSessionFactory")
//    public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDatasource") DataSource dataSource) throws Exception {
//        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//        bean.setDataSource(dataSource);
//        return bean.getObject();
//    }
//
//
//    @Bean(name = "slaveSqlSessionTemplate")
//    public SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
//        return new SqlSessionTemplate(sqlSessionFactory);
//    }
//
//    @Bean(name = "dynamicDataSource")
//    @Primary
//    public DynamicDataSource dataSource(@Qualifier("masterDatasource") DataSource masterDatasource,
//                                        @Qualifier("slaveDatasource") DataSource slaveDatasource) {
//        Map<Object, Object> targetDataSources = new HashMap<>();
//        targetDataSources.put("masterDatasource", masterDatasource);
//        targetDataSources.put("slaveDatasource", slaveDatasource);
//        DynamicDataSource dynamicDataSource = new DynamicDataSource();
//        dynamicDataSource.setTargetDataSources(targetDataSources);
//        dynamicDataSource.setDefaultTargetDataSource(masterDatasource);
//        return dynamicDataSource ;
//    }
//}
